var express = require('express');
var router = express.Router();
var fs = require("fs")
var upload = "public/uploads/"

var multer  = require('multer')
var upload = multer({ dest: upload})

var mysql = require('mysql');
		var conn = mysql.createConnection({
			host:'localhost',
			user:'root',
			password:'123456',
			database: 'projects',
			port: 3306
		});
	conn.connect();

/*	router.use(function (req, res, next) {
		if (req.session.is_login == true) {
			next()
		}else{
			res.redirect('/users/login')
		}
});*/

router.get('/', function(req, res, next) {
	conn.query('select *,articles.id as aid from articles join catalog on articles.cata_id = catalog.id order by aid desc',function(err,rows,fields){
		var article = rows
		if (err) throw err;
			res.render("articleList",{article:article, username:req.session.username,success:req.flash("success")})
			});
	})
router.get('/addArticle',function(req, res, next){
	conn.query('select * from catalog',function(err,rows,fields){
		res.render("addArticle",{catalogs:rows,error:req.flash("error")})
	})
})
router.post('/addArticle',function(req, res, next){
	var sql = 'insert into articles values(null,"'+req.body.title+'","'+req.body.content+'","'+req.body.img+'","'+req.body.cata_id+'")'
				if (req.body.title=="") {
				req.flash('error', '文章标题不能为空')
					res.redirect('/addmin/addArticle')
				}
				else if (req.body.img=="") {
					req.flash('error', '图片不能为空')
					res.redirect('/addmin/addArticle')
				}
				else if (req.body.content=="") {
					req.flash('error', '内容不能为空')
					res.redirect('/addmin/addArticle')
				}else if (req.body.content=="---请输入分类---") {
					req.flash('error', '请输入分类')
					res.redirect('/addmin/addArticle')
				}else{
				conn.query(sql,function(err,rows,fields){
				if (err) throw err;
					req.flash("success","提交文章成功")
					res.redirect('/addmin')
				});
			}
	});

router.get('/delarticle', function(req, res, next){
	var sql = 'delete from articles where id='+req.query.id
	conn.query(sql,function(err,rows,fields){
		if (err) throw err;
			res.redirect('/addmin')
		});
	});

router.get("/test",function(req,res,next){
	res.send("test")
})

router.get('/degArticle',function(req, res, next){
		conn.query('select * from catalog',function(err,rows,fields){
			var catalogs = rows
	conn.query('select * from articles where id='+req.query.id,function(err,rows,fields){
		console.log(rows)
		res.render("degArticle",{articles:rows[0],catalogs:catalogs})
		})
	})
})

router.post('/degArticle', function(req, res, next){
	var sql = 'update articles set  title= "'+req.body.title+'",content = "'+req.body.content+'",cata_id = "'+req.body.cata_id+'",img = "'+req.body.img+'"where id='+req.body.id
	conn.query(sql,function(err,rows,fields){
		if (err) throw err;
			res.redirect('/addmin')
		});
	});

router.post('/uploadFile',upload.single('fileInput'),function(req, res, next){
	fs.renameSync("public/uploads/"+req.file.filename,"public/uploads/"+req.file.originalname)
	var img = "/uploads/"+req.file.originalname
	res.send(img)
})

router.get("/loginout",function(req, res, next){
	req.session.destroy()
	res.redirect("/users/login");
})

module.exports = router;